package com.huangj.pro2_h2pop;


import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.Glide;
import com.huangj.entity.HomeInfoEntity;
import com.huangj.huangjlibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by HuangJ on 2016/10/6.15:53
 */
public class HomeInfoMapActivity extends BaseActivity {


    @BindView(R.id.map_rg)
    RadioGroup radioGroup;
    @BindView(R.id.map_btnall)
    RadioButton button_all;
    @BindView(R.id.map_btnlitle)
    RadioButton button_little;
    @BindView(R.id.map_hsv)
    HorizontalScrollView horizontalScrollView;
    @BindView(R.id.map_ll)
    LinearLayout linearLayout;

    MapView mMapView = null;
    BaiduMap mBaiduMap;
    double lat;
    double lng;
    private List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.DaysBean.PointsBean> pointsBeen;

    @Override
    protected int getContentId() {
        return R.layout.rotemap_layout;
    }

    @Override
    protected void init() {

        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();

        Intent intent = getIntent();
        final ArrayList<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.DaysBean> daysBean
                = (ArrayList<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.DaysBean>) intent.getSerializableExtra("datas");
        linearLayout.removeAllViews();
        for (int i = 0; i < daysBean.size(); i++) {

            pointsBeen = daysBean.get(i).getPoints();

            final  int starIndex = i;
            LinearLayout ll1 = new LinearLayout(this);
            ll1.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,90);
            lp1.setMargins(5,5,5,0);
            ll1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBaiduMap.clear();
                    List<LatLng> latLngList = new ArrayList<>();
                    for (int i1 = 0; i1 < daysBean.get(starIndex).getPoints().size(); i1++) {
                        final int index = i1;
                        LatLng latLng = new LatLng(daysBean.get(starIndex).getPoints().get(i1).getPoi().getLat(), daysBean.get(starIndex).getPoints().get(i1).getPoi().getLng());
                        //确定点;
                        getMapDW(daysBean.get(starIndex).getPoints().get(i1).getPoi().getLat(),daysBean.get(starIndex).getPoints().get(i1).getPoi().getLng());
                        //放入经纬度的集合;
                        latLngList.add(latLng);
                    }
                    //画线的方法；
                    getLins(latLngList);
                }
            });
            ll1.setLayoutParams(lp1);
            ll1.setBackgroundResource(R.color.colorw);
            ll1.setGravity(Gravity.CENTER);
            TextView tv1 = new TextView(this);
            tv1.setText("DAY"+daysBean.get(i).getPosition());
            tv1.setTextColor(Color.GRAY);
            ll1.addView(tv1);
            linearLayout.addView(ll1);
            for (int i1 = 0; i1 < daysBean.get(i).getPoints().size(); i1++) {
                final int index = i1;
                LinearLayout ll2= new LinearLayout(this);
                ll2.setOrientation(LinearLayout.VERTICAL);
                ll2.setGravity(Gravity.CENTER);
                ll2.setBackgroundResource(R.color.colorw);
                LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,90);
                lp2.setMargins(5,5,5,0);
                ll2.setLayoutParams(lp2);
                ll2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        Log.d("print", "经度: "+daysBean.get(starIndex).getPoints().get(index).getPoi().getLat()+" 纬度" +daysBean.get(starIndex).getPoints().get(index).getPoi().getLng());
                        lat = daysBean.get(starIndex).getPoints().get(index).getPoi().getLat();
                        lng = daysBean.get(starIndex).getPoints().get(index).getPoi().getLng();
                        //跳转地图;
                        getMapDW(lat,lng);
                        //点击跳出窗口；
                        showInfoWindow(index,daysBean.get(starIndex).getPoints());
                    }
                });
                TextView tv2 = new TextView(this);
                tv2.setText(i1+1+"");
                tv2.setGravity(Gravity.CENTER);
                TextView tv3 = new TextView(this);
                tv3.setText(daysBean.get(i).getPoints().get(i1).getPoi().getName());
                ll2.addView(tv2);
                ll2.addView(tv3);
                linearLayout.addView(ll2);
            }
        }

    }

    /**
     * 1、根据经纬度定位；绘制maker
     */
    public void getMapDW(double lat,double lng) {
        //定义Maker坐标点
        LatLng point = new LatLng(lat, lng);
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_map_green_digit);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap)
                .zIndex(9)  //设置marker所在层级
                .draggable(true);  //设置手势拖拽
        //在地图上添加Marker，并显示
                mBaiduMap.addOverlay(option);
        //将marker添加到地图上
        MapStatus mapStatus = new MapStatus.Builder().target(new LatLng(lat,lng)).zoom(15).build();
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        mBaiduMap.setMapStatus(mapStatusUpdate);
//        Marker marker = (Marker) (mBaiduMap.addOverlay(option));
}

    /**
     * 2、添加在地图中点击DAY  连线；
     */
    public void getLins(List<LatLng> latLngList){
//        Log.d("print", "getLins: "+latLngList.get(0).toString());
        OverlayOptions ooPolyline = new PolylineOptions().width(10)
                .color(Color.RED).points(latLngList);
        Polyline mPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline);
        mBaiduMap.addOverlay(ooPolyline);
    }

    /**
     * 点击地点显示窗口
     */

    public void showInfoWindow(int position,List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.DaysBean.PointsBean> pointsBeen){

        View view = LayoutInflater.from(this).inflate(R.layout.map_infowindow,null);
        TextView tv_area = (TextView) view.findViewById(R.id.area);
        TextView tv_topic = (TextView) view.findViewById(R.id.topic);
        TextView tv_tip = (TextView) view.findViewById(R.id.visit_tip);
        ImageView iv_cancle = (ImageView) view.findViewById(R.id.btn_cancle);

        ImageView iv = (ImageView) view.findViewById(R.id.iv);
        int imageId;
        switch (pointsBeen.get(position).getPoi().getPoi_type()) {
            case "SIGHT":
                imageId = R.drawable.icon_plan_scenery;
                break;
            case "RESTAURANT":
                imageId = R.drawable.icon_plan_food;
                break;
            default:
                imageId = R.drawable.icon_plan_hotel;
                break;
        }
        Glide.with(this)
                .load(imageId)
                .into(iv);

        tv_topic.setText(pointsBeen.get(position).getPoi().getName());
        tv_tip.setText("建议玩"+pointsBeen.get(position).getInspiration_activity().getVisit_tip());
        tv_area.setText(pointsBeen.get(position).getInspiration_activity().getDestination().getName() + "·");
        iv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBaiduMap.hideInfoWindow();
            }
        });
        //获得点坐标
        LatLng latLng = new LatLng(pointsBeen.get(position).getInspiration_activity().getPois().get(0).getLat(), pointsBeen.get(position).getInspiration_activity().getPois().get(0).getLng());
        //创建InfoWindow, 并把视图、 坐标 、偏移量传进去
        InfoWindow mInfoWindow = new InfoWindow(view,latLng, -100);
        //显示弹出窗
        mBaiduMap.showInfoWindow(mInfoWindow);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
}
